package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func Userinput() string {

	var mysqlCMD string
	fmt.Println("please input you mysql cmd >>>:")
	reader := bufio.NewReader(os.Stdin)

	mysqlCMD, _ = reader.ReadString('\n')
	return mysqlCMD
}

func Getinfo() (string, string) {
	/*
	  可能出现的情况,统一解法
	  mysql -hxxx -uroot -p123
	  mysql -h xxx -uroot -p123
	  mysql -h xxx -u root -p123
	  mysql -h xxx -uroot -p 123
	*/
	mysqlCMD := Userinput()
	noSpaceCMD := strings.Replace(mysqlCMD, " ", "", -1) // 给cmd去重

	startUserIndex := strings.Index(noSpaceCMD, "-u") + 2     // user索引起始位置
	endUserIndex := strings.Index(noSpaceCMD, "-p")           // user索引结束位置
	startPasswordIndex := strings.Index(noSpaceCMD, "-p") + 2 // password起始位置

	mysqlUsername := noSpaceCMD[startUserIndex:endUserIndex]
	mysqlPassword := noSpaceCMD[startPasswordIndex:]
	return mysqlUsername, mysqlPassword
}

func Checkuser() bool {
	mysqlUsername, mysqlPassword := Getinfo()
	if mysqlUsername == "yuan" && mysqlPassword == "123\n" {
		return true
	} else {
		return false
	}

}

func main() {
	result := Checkuser()
	if result {
		fmt.Println("login success!")
	} else {
		fmt.Println("login fail password or username wrong!")
	}
}
